package org.budo.graph.entity;

import java.util.LinkedHashMap;
import java.util.Map;

import org.budo.support.lang.util.NumberUtil;

import lombok.Getter;
import lombok.Setter;

/**
 * @SqlGraph
 * @author limingwei
 */
@Getter
@Setter
public class SqlGraph {
    public static final String GRAPH_TYPE = "SQL";

    private static final String EXECUTE_COUNT = "ExecuteCount", TOTAL_TIME = "TotalTime", FETCH_ROW_COUNT = "FetchRowCount", EFFECTED_ROW_COUNT = "EffectedRowCount";

    public static final String[] TITLES = { EXECUTE_COUNT, TOTAL_TIME, FETCH_ROW_COUNT, EFFECTED_ROW_COUNT };

    private Long executeCount;

    private Long totalTime;

    private Long fetchRowCount;

    private Long effectedRowCount;

    public SqlGraph(Map<String, Object> sqlStaMapt) {
        Object executeCount = sqlStaMapt.get(EXECUTE_COUNT);
        this.setExecuteCount(NumberUtil.toLong(executeCount, 0L));

        Object totalTime = sqlStaMapt.get(TOTAL_TIME);
        this.setTotalTime(NumberUtil.toLong(totalTime, 0L));

        Object fetchRowCount = sqlStaMapt.get(FETCH_ROW_COUNT);
        this.setFetchRowCount(NumberUtil.toLong(fetchRowCount, 0L));

        Object effectedRowCount = sqlStaMapt.get(EFFECTED_ROW_COUNT);
        this.setEffectedRowCount(NumberUtil.toLong(effectedRowCount, 0L));
    }

    public Map<String, String> minus(SqlGraph lastSqlGraph) {
        Map<String, String> minusMap = new LinkedHashMap<String, String>(); // 这里应该是需要有序的Map的，顺序应该和 TITLES 一致
        minusMap.put(EXECUTE_COUNT, (this.getExecuteCount() - lastSqlGraph.getExecuteCount()) + ""); // 这里存储的是增量
        minusMap.put(TOTAL_TIME, (this.getTotalTime() - lastSqlGraph.getTotalTime()) + ""); // 这里存储的是增量
        minusMap.put(FETCH_ROW_COUNT, (this.getFetchRowCount() - lastSqlGraph.getFetchRowCount()) + ""); // 这里存储的是增量
        minusMap.put(EFFECTED_ROW_COUNT, (this.getEffectedRowCount() - lastSqlGraph.getEffectedRowCount()) + ""); // 这里存储的是增量
        return minusMap;
    }
}